/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rockville.testing;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

/**
 *
 * @author Usman
 */
public class parserTest {
    
    
    
    public static void main(String[] arg) {
    	String literal = "- Fruit chat -\n\nIngredients:\n\n* 1 cup melon\n* 1 apple\n* 3 bananas\n* 1/2 cup grapes\n";
    	literal = literal.replaceFirst("(.*):", "<b>$1:</b>");
    	literal = literal.replaceFirst("\\-(.*)\\-", "<div style=\"font-weight:bold;\">$1</div>");
    	literal = literal.replaceAll("\n", "<br/>");    	
    	System.out.println(literal);
    	
    	
//    	literal = "<div><div class=\"temperature\"><span class=\"condition\"> Haze </span>32 C</div><div class=\"summary\">High: 36 C Low: 26 C</div><table class=\"tbl_forecast\"><tr><th>Today</th><th>Tomorrow</th><th>Thu</th><th>Fri</th><th>Sat</th></tr><tr class=\"fiveday-icons\"><td><img id=\"wiff\" style=\"background-position: -1952px -0px; width: 61px; height: 34px\"  alt=\"\" src=\"http://l.yimg.com/a/lib/ywc/img/spacer.gif\"/><br/>Sunny</div></td><td><img id=\"wiff\" style=\"background-position: -1952px -0px; width: 61px; height: 34px\"  alt=\"\" src=\"http://l.yimg.com/a/lib/ywc/img/spacer.gif\"/><br/>Sunny</div></td><td><img id=\"wiff\" style=\"background-position: -1952px -0px; width: 61px; height: 34px\"  alt=\"\" src=\"http://l.yimg.com/a/lib/ywc/img/spacer.gif\"/><br/>Sunny</div></td><td><img id=\"wiff\" style=\"background-position: -1952px -0px; width: 61px; height: 34px\"  alt=\"\" src=\"http://l.yimg.com/a/lib/ywc/img/spacer.gif\"/><br/>Sunny</div></td><td><img id=\"wiff\" style=\"background-position: -1952px -0px; width: 61px; height: 34px\"  alt=\"\" src=\"http://l.yimg.com/a/lib/ywc/img/spacer.gif\"/><br/>Sunny</div></td></tr><tr class=\"fiveday-temps\"><td>High: 36 C <div>Low: 26 C</div></td><td>High: 37 C <div>Low: 25 C</div></td><td>High: 36 C <div>Low: 26 C</div></td><td>High: 36 C <div>Low: 25 C</div></td><td>High: 36 C <div>Low: 25 C</div></td></tr></table></div>";
    	literal = "<div><div class=\"temperature\"><span class=\"condition\"> Haze </span>32 C</div><div class=\"summary\">High: 36 C Low: 26 C</div><table class=\"tbl_forecast\"><tr><th>Today</th><th>Tomorrow</th><th>Thu</th><th>Fri</th><th>Sat</th></tr><tr class=\"fiveday-icons\"><td><img id=\"wiff\" style=\"background-position: -1952px -0px; width: 61px; height: 34px\"  alt=\"\" src=\"http://l.yimg.com/a/lib/ywc/img/spacer.gif\"/><br/>Sunny</div></td><td><img id=\"wiff\" style=\"background-position: -1952px -0px; width: 61px; height: 34px\"  alt=\"\" src=\"http://l.yimg.com/a/lib/ywc/img/spacer.gif\"/><br/>Sunny</div></td><td><img id=\"wiff\" style=\"background-position: -1952px -0px; width: 61px; height: 34px\"  alt=\"\" src=\"http://l.yimg.com/a/lib/ywc/img/spacer.gif\"/><br/>Sunny</div></td><td><img id=\"wiff\" style=\"background-position: -1952px -0px; width: 61px; height: 34px\"  alt=\"\" src=\"http://l.yimg.com/a/lib/ywc/img/spacer.gif\"/><br/>Sunny</div></td><td><img id=\"wiff\" style=\"background-position: -1952px -0px; width: 61px; height: 34px\"  alt=\"\" src=\"http://l.yimg.com/a/lib/ywc/img/spacer.gif\"/><br/>Sunny</div></td></tr><tr class=\"fiveday-temps\"><td>High: 36 C <div>Low: 26 C</div></td><td>High: 37 C <div>Low: 25 C</div></td><td>High: 36 C <div>Low: 26 C</div></td><td>High: 36 C <div>Low: 25 C</div></td><td>High: 36 C <div>Low: 25 C</div></td></tr></table></div>";
   		literal = literal.replaceAll("\'", "\"");
   		literal = literal.replaceAll("\"", "\\\\\"");
    	System.out.println(literal);
    	
    	literal = "Freeman to receive Life Achievement Award\n\nLOS ANGELES: The American Film Institute is honoring Morgan Freeman with its Life Achievement Award.\nAFI says the 73-year-old Oscar winner will receive the award in June.\nAFI chair Sir Howard Stringer calls Freeman";
    	int index = literal.indexOf("\n");
    	if(index > 0)
    		System.out.println(literal.substring(0, index));
    	else 
    		System.out.println(index);
    	//    	createDocument();
    }	
    
    public void makeConnection() {    	
         Connection conn = null;
         PreparedStatement ps = null;
         Statement cs = null;
         Statement cs1 = null;
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         try {
             Class.forName("com.mysql.jdbc.Driver");
        	 conn = DriverManager.getConnection("jdbc:mysql://192.168.1.119:3306/telenor", "root", "iceland");
        	 
        	 String sql = "INSERT INTO `ramzan_timings` (`ramzan_day`, `gregorian_day`, `sehr`, `iftaar`, `default_city`) VALUES (?, ?, ?, ?, ?)";
        	 ps = conn.prepareStatement(sql);
             cs = conn.createStatement();
             ResultSet rs = cs.executeQuery("select city, time_plus from cities_time_diff");
             int c = 0;
             while (rs.next()) {
            	 String city = rs.getString(1);
            	 int timeToAdd = rs.getInt(2);
                 
            	 cs1 = conn.createStatement();
                 ResultSet rs1 = cs.executeQuery("select `ramzan_day`, `gregorian_day`, `sehr`, `iftaar`, `default_city` from `ramzan_timings` where default_city='Lahore' order by ramzan_day;");
                 while (rs1.next()) {
                	 int ramzanDay = rs.getInt(1);
                	 String gregDay = rs.getString(2);
                	 String sehr = rs.getString(3);
                	 String iftaar = rs.getString(4);
                	 Date sehrTime = sdf.parse(gregDay+" "+sehr);
                	 Date iftaarTime = sdf.parse(gregDay+" "+iftaar);
                	 Calendar sehrCal = new GregorianCalendar();
                	 sehrCal.clear();
                	 sehrCal.setTime(sehrTime);
                	 sehrCal.add(Calendar.MINUTE, timeToAdd);
                	 Calendar iftaarCal = new GregorianCalendar();
                	 iftaarCal.clear();
                	 iftaarCal.setTime(iftaarTime);
                	 iftaarCal.add(Calendar.MINUTE, timeToAdd);
                	 
                	 ps.setInt(1, ramzanDay);
                	 ps.setString(2, gregDay);
                	 
                 }
            	 
             }
             cs.close();
         } catch (Exception e) {
             e.printStackTrace();
         } finally {
             if(conn != null)
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
         }    	
    }
    
    public static void createDocument() {
    	DocumentBuilderFactory docFactory = null;
    	DocumentBuilder docBuilder = null;

    	Document xmlDoc = null;
    	File xmlFile = null;
    	String filePath = "C:\\Documents and Settings\\Administrator\\My Documents\\Google Talk Received Files\\RDM_football_live_73148_0370.xml";
    	System.out.println(filePath);
    	try {

    	xmlFile = new File(filePath);
    	
    	docFactory = DocumentBuilderFactory.newInstance();

    	docBuilder = docFactory.newDocumentBuilder();
    	try {

    	xmlDoc = docBuilder.parse(xmlFile);

    	} catch (SAXException sAXException) {
    	System.out.println("SAX Exception");
    	sAXException.printStackTrace();

    	} catch (IOException iOException) {
    	System.out.println("IO Exception");
    	iOException.printStackTrace();
    	}


    	} catch (Exception e) {
    	System.out.print("Exception! --- ServiceManager : getxmlDocument()");
    	e.printStackTrace();
    	}
//    	return xmlDoc;

    }
    
}
